<?php
/**
 * Created by PhpStorm.
 * User:lyq
 * Date: 2018/8/25
 * Time: 17:08
 */

namespace app\admin\controller;

use think\Exception;
use think\Db;
use think\Session;

class Message extends Common
{
    public function index(){
        //标题
        $this->templateTitle("会员对话记录");

        $where['me.id'] = ['gt',0] ;

        //搜索条件-手机
        $condition = request()->param("condition");
        $this->assign("condition", $condition);
        if ($condition)
        {
            $where["send.mobile|send.real_name|send.id|get.mobile|get.real_name|get.id"] = $condition;
        }

        //搜索条件-时间
        if (request()->has("time_range"))
        {
            //解析时间
            $timeRange = request()->param("time_range");
            $timeRange = explode(" ~ ", $timeRange);
            //模板渲染值
            $this->assign("range_start", ["time_range" => $timeRange[0]]);
            $this->assign("range_end", ["time_range" => $timeRange[1]]);

            $where['me.ctime'] = [
                ["EGT", strtotime($timeRange[0] . " 00:00:00")],
                ["ELT", strtotime($timeRange[1] . " 23:59:59")]
            ];

        }
        else
        {
            //模板渲染值
            $this->assign("range_start", ["time_range" => '1970-01-01']);
            $this->assign("range_end", ["time_range" => date("Y-m-d")]);
        }

        //获取所有用户
        $list = Db::name("message")
            ->alias("me")
            ->join("__USER__ send", "send.id=me.send_uid",'LEFT')
            ->join("__USER__ get", "get.id=me.get_uid",'LEFT')
            ->where($where)
            ->order("me.ctime DESC")
            ->field('me.*,send.mobile as smobile,send.real_name as sname,send.avatar as savatar,get.mobile as gmobile,get.real_name as gname,get.avatar as gavatar')
            ->group('me.sorts')
            ->paginate(10);

        $this->assign("list", $list);

        return $this->fetch();
    }

    public function detail(){
        if (request()->isGet())
        {
            $this->layoutModal();
            $sorts = request()->param('sorts');
            $this->assign('sorts',$sorts);

            $where['me.id']=['gt',0];

            //搜索条件-时间
            if (request()->has("time_range"))
            {
                //解析时间
                $timeRange = request()->param("time_range");
                $timeRange = explode(" ~ ", $timeRange);
                //模板渲染值
                $this->assign("range_start", ["time_range" => $timeRange[0]]);
                $this->assign("range_end", ["time_range" => $timeRange[1]]);

                $where['me.ctime'] = [
                    ["EGT", strtotime($timeRange[0] . " 00:00:00")],
                    ["ELT", strtotime($timeRange[1] . " 23:59:59")]
                ];

            }
            else
            {
                //模板渲染值
                $this->assign("range_start", ["time_range" => '1970-01-01']);
                $this->assign("range_end", ["time_range" => date("Y-m-d")]);
            }

            $list = Db::name('message')
                ->alias('me')
                ->join("__USER__ send", "send.id=me.send_uid",'LEFT')
                ->where('me.sorts',$sorts)
                ->where($where)
                ->field('me.*,send.mobile as smobile,send.real_name as sname,send.avatar as savatar')
                ->order('me.id asc')
                ->paginate(20);
            $this->assign('list',$list);

            return $this->fetch();
        }
    }

    public function remove(){
        if(request()->isAjax()){
            $type = request()->param('type');
            $deltag = request()->param('deltag');

            if($type==1){
                $where['sorts'] = $deltag;
            }elseif($type==2){
                $where['id'] = $deltag;
            }else{
                return $this->ajaxError('参数有误');
            }

            $del = Db::name('message')
                ->where($where)
                ->delete();
            if($del){
                return $this->ajaxSuccess('删除成功');
            }else{
                return $this->ajaxError('删除失败');
            }
        }
    }
}

